package com.hellogood.yaohuo.client.setting;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.shared.entity.GroupUser;
import com.hellogood.yaohuo.shared.entity.User;
import com.hellogood.yaohuo.shared.services.GroupUserService;
import com.hellogood.yaohuo.shared.services.UserService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.TransferImgButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;

public class GroupSettingWindow extends Window {
	private String groupCode = null;
	private String groupName = null;

	private ListGrid unClassfiedUserListGrid = null;
	private ListGrid userUnderGroupListGrid = null;
	private Button btn = null;

	public GroupSettingWindow(String groupCode, String groupName) {
		this.groupCode = groupCode;
		this.groupName = groupName;
		initData();
		initView();
	}

	private void initData() {
		UserService.Util.getInstance().getAllUserList(new AllUserListCallback());		
		GroupUserService.Util.getInstance().getGoupUserByGroupCode(groupCode, new UserListByGroupCallback());
	}

	private void initView() {
		ListGridField code1Field = new ListGridField("code");
		code1Field.setHidden(true);
		ListGridField name1Field = new ListGridField("name", "组织");
		unClassfiedUserListGrid = new ListGrid();
		unClassfiedUserListGrid.setWidth(300);
		unClassfiedUserListGrid.setHeight(300);
		unClassfiedUserListGrid.setCanDragRecordsOut(true);
		unClassfiedUserListGrid.setDragDataAction(DragDataAction.MOVE);
		unClassfiedUserListGrid.setFields(code1Field, name1Field);

		ListGridField code2Field = new ListGridField("code");
		code2Field.setHidden(true);
		ListGridField name2Field = new ListGridField("name", "组织");
		userUnderGroupListGrid = new ListGrid();
		userUnderGroupListGrid.setWidth(300);
		userUnderGroupListGrid.setHeight(300);
		userUnderGroupListGrid.setCanDragRecordsOut(true);
		userUnderGroupListGrid.setDragDataAction(DragDataAction.MOVE);
		userUnderGroupListGrid.setCanAcceptDroppedRecords(true);
		userUnderGroupListGrid.setCanRemoveRecords(true);
		userUnderGroupListGrid.setPreventDuplicates(true);
		userUnderGroupListGrid.setFields(code2Field, name2Field);

		HStack hStack = new HStack(10);
		hStack.setHeight(350);

		Label label = new Label();
		label.setHeight(50);
		label.setContents("未分配组织");
		VStack vStack = new VStack();
		vStack.addMember(label);
		vStack.addMember(unClassfiedUserListGrid);
		hStack.addMember(vStack);

		TransferImgButton arrowImg = new TransferImgButton(TransferImgButton.RIGHT);
		arrowImg.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				unClassfiedUserListGrid.transferSelectedData(userUnderGroupListGrid);
			}
		});
		hStack.addMember(arrowImg);

		Label label2 = new Label();
		label2.setHeight(50);
		label2.setContents(groupName);
		VStack vStack2 = new VStack();
		vStack2.addMember(label2);
		vStack2.addMember(userUnderGroupListGrid);

		hStack.addMember(vStack2);

		hStack.setSize("100%", "100%");

		HLayout hLayout = new HLayout();
		btn = new Button("保存");
		btn.setAlign(Alignment.CENTER);
		btn.addClickHandler(new SaveBtnClickHandler());
		hLayout.setAlign(Alignment.CENTER);
		hLayout.addMember(btn);
		hLayout.setVertical(true);
		hLayout.setPadding(20);

		VLayout layout = new VLayout();
		layout.setTop(10);
		layout.setLeft(60);
		layout.setMembers(hStack, hLayout);
		addChild(layout);
	}

	private class AllUserListCallback implements AsyncCallback<List<User>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<User> result) {
			if (result != null) {
				Record[] records = new Record[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Record r = new Record();
					r.setAttribute("code", result.get(i).getStaffCode());
					r.setAttribute("name", result.get(i).getStaffName());
					records[i] = r;
				}
				unClassfiedUserListGrid.setData(records);
			}
		}
	}

	private class UserListByGroupCallback implements AsyncCallback<List<GroupUser>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<GroupUser> result) {
			if (result != null) {
				Record[] records = new Record[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Record r = new Record();
					r.setAttribute("code", result.get(i).getStaffCode());
					r.setAttribute("name", result.get(i).getStaffName());
					records[i] = r;
				}
				userUnderGroupListGrid.setData(records);
			}
		}

	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			ListGridRecord[] records = userUnderGroupListGrid.getRecords();
			if (records != null) {
				List<User> userList = new ArrayList<User>();
				for (ListGridRecord r : records) {
					User user = new User();
					user.setStaffCode(r.getAttribute("code"));
					user.setStaffName(r.getAttribute("name"));
					userList.add(user);
				}
				GroupUserService.Util.getInstance().updateUserListForGroup(userList, groupCode, new SaveUserListGroupCallback());
			}
		}
	}

	private class SaveUserListGroupCallback implements AsyncCallback<String> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(String result) {
			if (result == null) {
				SC.say("保存错误");
			} else if (result.equals("")) {
				SC.say("保存成功");
				btn.setDisabled(true);
			} else {
				SC.say(result);
			}
		}
	}
}
